import { Router } from 'express';
import Payload from '../models/Payload';
// import productManager from '../services/ProductManagerService';
import redisUtil from '../lib/RedisUtil';
export default () => {
    let api = Router();

    // redis新增测试
    api.post('/postredis', async (req, res, next) => {
        let redisData = req.body;
        console.log("key:" + redisData.value);
        let value = redisData.value;
        console.log("value:" + redisData.key);
        let key = redisData.key;
        try {
            if (redisData.value && redisData.key){
                let result = await redisUtil.set(key, value);
                if (redisData.time == null) {
                    console.log("time is null ");
                } else {
                    redisUtil.expire(key, redisData.time);
                    console.log("time is " + redisData.time);
                }
                res.status(200).json(new Payload(result));
            }else{
                const messages = 'key或者value有误';
                res.status(200).json(new Payload(messages));
            }
        } catch (error) {
            next(error);
        }
    });

    // redis删除测试
    api.delete('/deleteredis', async (req, res, next) => {
        let key = req.body.key;
        console.log("key:" + key);
        let status = true;
        try {
            let result = await redisUtil.del(key);
            if (result) {
                console.log('status:' + status);
            } else {
                status = false;
                console.log('status:' + status);
            }
            res.status(200).json(new Payload(status));
        } catch (error) {
            next(error);
        }
    });

    return api;
}
